package adapter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import util.Config;
import util.Utility;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

public class JsonParse {
	public static final String url = "http://nb-mobileapi.vbs.vn/youtube/Cat.php";
	
	private static JsonParse INSTANCE = new JsonParse();
	public static JsonParse getInstance() {
		if(INSTANCE==null){
			INSTANCE = new JsonParse();
		}		
		return INSTANCE;
	}
	//get json from url and namvaluepair (parmams: url, namevaluepair)
	private String getJsonFromURLPostNameValuePair(String url, List<NameValuePair> doiso) {
		StatusLine status = null;
		HttpParams httpParameters = new BasicHttpParams();
		// Set the timeout in milliseconds until a connection is
		// established.
		int timeoutConnection = 10000;
		HttpConnectionParams.setConnectionTimeout(httpParameters,
				timeoutConnection);
		// Set the default socket timeout (SO_TIMEOUT)
		// in milliseconds which is the timeout for waiting for data.
		int timeoutSocket = 10000;
		HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

		HttpClient client = new DefaultHttpClient(httpParameters);
		HttpPost post = new HttpPost(url);
		String noidung = "";
		try {
			UrlEncodedFormEntity e = new UrlEncodedFormEntity(doiso,
					"UTF-8");
			post.setEntity(e);
			ResponseHandler<String> handler = new BasicResponseHandler();
			status = client.execute(post).getStatusLine();
			if (status.getStatusCode() == HttpStatus.SC_OK) {
				noidung = client.execute(post, handler);
			}
			else{
				noidung = "busy";
			}
			
		} catch (Exception e) {
			//Log.d("error",e.getMessage());
			e.printStackTrace();
			noidung = "busy";
		}
		return noidung;		
	}

	public String getVideo(Context sherlockActivity,
			String video) {
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "getvideo"));//getnoteexpense
		value.add(new BasicNameValuePair("video", video));
		
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json;
	}
	public String getMyApp(Context sherlockActivity) {
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "getmyapp"));//getnoteexpense
		
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json;
	}
	public String searchVideo(Context sherlockActivity, String keyWord, int page) {
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "searchvideo"));//getnoteexpense
		value.add(new BasicNameValuePair("page", page+""));//getnoteexpense
		value.add(new BasicNameValuePair("catid", Config.appCat));//getnoteexpense
		value.add(new BasicNameValuePair("expression", keyWord));//getnoteexpense
		//value.add(new BasicNameValuePair("dpi", Utility.getDPI(sherlockActivity)));//getnoteexpense
		
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json+".."+page);
		return json;
	}
	public String getVideo(Context applicationContext, String menuID, int page) {
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		if(menuID.equalsIgnoreCase(Config.mostLiked)){
			value.add(new BasicNameValuePair("command", "getmorelikes"));//getnoteexpense
		}
		else if(menuID.equalsIgnoreCase(Config.mostlatest)){
			value.add(new BasicNameValuePair("command", "getmorelatest"));//getnoteexpense
		}
		else if(menuID.equalsIgnoreCase(Config.mostviewed)){
			value.add(new BasicNameValuePair("command", "getmoreviews"));//getnoteexpense
		}
		
		
		value.add(new BasicNameValuePair("page", page+""));//getnoteexpense
		value.add(new BasicNameValuePair("catid", Config.appCat));//getnoteexpense		
		//value.add(new BasicNameValuePair("dpi", Utility.getDPI(applicationContext)));//getnoteexpense
		
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json+".."+page);
		return json;
	}
	//gmail,username,nickname,pass,isGender+"",mYear+"-"+mMonth+"-"+mDay,"Platform",gcmId
	public String registerAccount(String[] params, Context context) {
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "register"));
		value.add(new BasicNameValuePair("gmail", params[0]));
		//value.add(new BasicNameValuePair("username", params[1]));
		value.add(new BasicNameValuePair("nickname", params[2]));
		value.add(new BasicNameValuePair("password", params[3]));
		value.add(new BasicNameValuePair("birthday", params[5]));
		/*value.add(new BasicNameValuePair("platform", params[6]));
		value.add(new BasicNameValuePair("deviceid", params[7]));
		*/
		value.add(new BasicNameValuePair("type", Config.typeServer));
		value.add(new BasicNameValuePair("gender", params[4]));
		
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json;
	}
	public String loginAccount(String[] params, Context c)
	{//gmail,pass,platform,gcmId,type
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "login"));
		value.add(new BasicNameValuePair("gmail", params[0]));
		value.add(new BasicNameValuePair("password", params[1]));
		
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json; 
	}
	//nickname, gender, birthday, userid
	public String updateinfor(String[] params, Context context) {
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "updatemyprofile"));
		value.add(new BasicNameValuePair("userid", params[3]));
		value.add(new BasicNameValuePair("birthday", params[2]));
		value.add(new BasicNameValuePair("nickname", params[0]));
		value.add(new BasicNameValuePair("gender", params[1]));
		value.add(new BasicNameValuePair("oldpass", params[4]));
		value.add(new BasicNameValuePair("newpass", params[5]));
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json;
		
	}
	public String SendEmailForgetPassword(String email)
	{
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "forgetpassword"));
		value.add(new BasicNameValuePair("gmail", email));
		
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json; 
	}
	public String IncreaLike(String videoID, String isLike, String userId) {
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "is_increate_like"));
		value.add(new BasicNameValuePair("id", videoID));
		value.add(new BasicNameValuePair("userid", userId));
		value.add(new BasicNameValuePair("islike", isLike));
		
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json;
	}
	public String getComment(String videoID, int page) {
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "getcomment"));
		value.add(new BasicNameValuePair("videoid", videoID));
		value.add(new BasicNameValuePair("page", page+""));
		
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json); 
		return json;
	}
	public String sendComment(String videoID, String userID, String comment) {
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "commentvideo"));
		value.add(new BasicNameValuePair("videoid", videoID));
		value.add(new BasicNameValuePair("comment", comment));
		value.add(new BasicNameValuePair("userid", userID));
		
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json;
	}
	public String loginAccountWithSocial(String[] params,
			Context loginActivity) {
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "loginsocial"));
		value.add(new BasicNameValuePair("gmail", params[0]));
		//value.add(new BasicNameValuePair("username", params[1]));
		value.add(new BasicNameValuePair("birthday", params[2]));
		value.add(new BasicNameValuePair("nickname", params[3]));
		try {
			int i = Integer.parseInt(params[4]);
			value.add(new BasicNameValuePair("gender", params[4]));
		} catch (Exception e) {
			value.add(new BasicNameValuePair("gender", "0"));
		}
		//value.add(new BasicNameValuePair("logo", params[5]));
		value.add(new BasicNameValuePair("type", params[6]));
		//value.add(new BasicNameValuePair("platform", params[7]));
		//value.add(new BasicNameValuePair("deviceid", params[8]));
		
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json;
	}
	public String sendView(String videoID) {
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "incrementviews"));
		value.add(new BasicNameValuePair("videoid", videoID));
		
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json;
	}
	public String getVideoLink(Context applicationContext, String videoID) {
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "getlink"));
		value.add(new BasicNameValuePair("link", videoID));
		
		String json = getJsonFromURLPostNameValuePair(url,value);
		Log.d("json",json);
		return json;
	}
	public void removeVideo(Context applicationContext, String videoID) {
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "remove_video"));
		value.add(new BasicNameValuePair("videoid", videoID));
		
		String json = getJsonFromURLPostNameValuePair(url,value);
	}
	public void increateView(String videoID) {
		List<NameValuePair> value = new ArrayList<NameValuePair>();
		value.add(new BasicNameValuePair("command", "increate_view"));
		value.add(new BasicNameValuePair("id", videoID));
		
		getJsonFromURLPostNameValuePair(url,value);
		
	}
}
